﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KL.Store.Web.Models;
using KL.Store.Data;
using KL.Store.Core.Domain;
using KL.Store.Core;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace KL.Store.Web.Controllers
{
    [Authorize]
    public class WishListController : KL.Store.Core.Web.BaseController
    {
        #region Index

        public ActionResult Index()
        {
            return View();
        }

        #endregion

        #region Get

        [Child]
        public ActionResult Get(long? id)
        {
            WishListModel wishListModel = new WishListModel();

            if (eStoreUser.AccountId > 0 && id.HasValue)
            {
                WishList wl = dbWishLists.Get(eStoreUser.AccountId, (long)id);
                if (wl != null)
                {
                    Mapper.MapDomainToViewModel2(wl, wishListModel);
                }
            }
            if (id.HasValue && wishListModel.ListId == 0) wishListModel.FormMessage = "<script type='text/javascript'> window.location.href = '/WishLists/Index';</script>";

            return PartialView("Get", wishListModel);
        }

        #endregion

        #region Edit

        [HttpPost]
        public ActionResult Edit(WishListModel wl, string button)
        {
            if (eStoreUser.AccountId > 0)
            {
                if (wl.ListId > 0)
                {
                    dbWishLists.Update(wl.UserId, wl.ListId, wl.ListName, wl.Explanation, wl.FirstName, wl.LastName, wl.Email, wl.Phone, wl.Address_Name, wl.Address_Phone,
                                       wl.Address_City, wl.Address_County, wl.Address, wl.ListUrl, wl.DeliveryByReceive, wl.DeliveryDate, wl.ListPass, wl.Published, wl.Voucher,
                                       wl.Delegate, wl.Delegate_FirstName, wl.Delegate_LastName, wl.Delegate_Phone, wl.Delegate_Email, eStoreUser.AccountId);
                }
                else
                {
                    wl.UserId = eStoreUser.AccountId;
                    long id = dbWishLists.Insert(wl.UserId, wl.ListName, wl.Explanation, wl.FirstName, wl.LastName, wl.Email, wl.Phone, wl.Address_Name, wl.Address_Phone,
                                                 wl.Address_City, wl.Address_County, wl.Address, wl.ListUrl, wl.DeliveryByReceive, wl.DeliveryDate, wl.ListPass, wl.Published, wl.Voucher,
                                                 wl.Delegate, wl.Delegate_FirstName, wl.Delegate_LastName, wl.Delegate_Phone, wl.Delegate_Email, eStoreUser.AccountId);
                    wl.ListId = id;
                }
                if (!string.IsNullOrEmpty(button))
                {
                    int count = 0;
                    if (button == "FaceBook")
                    {

                    }
                    else if (button == "Twitter")
                    {

                    }
                    else if (button == "Msn")
                    {

                    }
                    else if (button == "Google")
                    {

                    }
                    else if (button == "Email")
                    {
                        IList<Contact> Contacts = dbContact.GetList(eStoreUser.AccountId, "");
                        string Link = Parameters.WebSiteUrl + "/WishList/ListDetail/" + wl.ListId.ToString();
                        Link = "<a href='" + Link + ">" + wl.ListName + "'</a>";
                        string body = dbRefs.GetRef5(135).Replace("{User}",wl.FirstName + " " + wl.LastName).Replace("{Link}",Link);

                        //Merhaba,
                        //{User} kullanıcısı tarafından oluşturulan istek listesine aşağıdaki linki kullanarak ulaşabilirsiniz.
                        //İstek Listesi Linki :  {Link}
                        //İyi alış verişler.

                        int say = 0;
                        foreach(Contact contact in Contacts)
                        {
                            try
                            {
                                //Utils.SendMail(contact.Email, "Hediye Listem", body);
                                say++;
                            }
                            catch (Exception ex)
                            {
                                Logger.Write(ex.Message, "Exception");
                            }
                        }
                        count = say;
                    }
                    else if (button == "Sms")
                    {
                        IList<Contact> Contacts = dbContact.GetList(eStoreUser.AccountId, "");
                        count = Contacts.Count;
                    }
                    dbWishLists.Publish(eStoreUser.AccountId, wl.ListId, button, count, eStoreUser.AccountId);
                }
            }

            //return PartialView("Get", wl);
            return Get(wl.ListId);
        }

        #endregion

        #region List

        public ActionResult List(int ProductId)
        {
            Product prd = dbProduct.Get(ProductId, eStoreUser.AccountId);
            AddToWishListModel atwlModel = new AddToWishListModel();
            atwlModel.ProductId = prd.ProductId;
            atwlModel.ProductName = prd.ProductName;

            atwlModel.WishLists = dbWishLists.ProductWishList(eStoreUser.AccountId, ProductId);

            return View(atwlModel);
        }

        #endregion

        #region Set

        [HttpPost]
        public ActionResult Set(AddToWishListModel awl)
        {
            int Result = 0;
            string retString = "Beklenmeyen bir hata oluştu !";
            if (eStoreUser.AccountId > 0)
            {
                if (awl.SelectedListChecked)
                {
                    #region Add To List

                    Result = dbWishListDetail.Insert(eStoreUser.AccountId, awl.SelectedList, awl.ProductId, awl.Qty);
                    retString = "<font color=green>Ürün hediye listenize eklendi.</font>";

                    #endregion
                }
                else
                {
                    #region Remove From List

                    Result = dbWishListDetail.Delete(eStoreUser.AccountId, awl.SelectedList, awl.ProductId);
                    retString = "<font color=red>Ürün hediye listenizden kaldırıldı.</font>";

                    #endregion
                }
            }

            return Content(retString);
        }

        #endregion

        #region Update

        [Child]
        public ActionResult Update(int listId, int id, decimal? qty)
        {
            if (eStoreUser != null && eStoreUser.AccountId > 0 && id > 0 && listId >= 0 && qty.HasValue)
            {
                dbWishListDetail.Update(eStoreUser.AccountId, listId, id, (decimal)qty, eStoreUser.AccountId);
            }

            return GetListDetail(listId);
        }

        #endregion

        #region Remove

        [Child]
        public ActionResult Remove(int listId, int id)
        {
            int Result = 0;
            if (eStoreUser != null && eStoreUser.AccountId > 0 && listId > 0 && id > 0)
            {
                Result = dbWishListDetail.Delete(eStoreUser.AccountId, listId, id);
            }

            return GetListDetail(listId);
        }

        #endregion

        #region ListDetail

        public ActionResult ListDetail()
        {
            return View();
        }

        #endregion

        #region GetListDetail

        [Child]
        public ActionResult GetListDetail(long? id)
        {
            WishListDetailModel wldModel = new WishListDetailModel();
            wldModel.ListId = -1;
            wldModel.ListName = "Liste Bulunamadı!";

            if (eStoreUser.AccountId > 0 && id.HasValue)
            {
                #region Find List

                WishList wl = new WishList();
                wl = dbWishLists.Get(eStoreUser.AccountId, (long)id);
                wldModel.ListId = wl.ListId;
                wldModel.ListName = wl.ListName;

                #endregion

                IList<WishListDetail> wld = dbWishListDetail.GetList(eStoreUser.AccountId, (long)id);
                if (wld != null)
                {
                    wldModel.Products = wld;
                    //Mapper.MapDomainToViewModel2(wld, wishListModel);
                }
            }
            //if (id.HasValue && wishListModel.ListId == 0) wishListModel.FormMessage = "<script type='text/javascript'> window.location.href = '/WishLists/Index';</script>";

            return PartialView("GetListDetail", wldModel);
        }

        #endregion
    }
}
